using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BoosterPanel : BasePanel, IInitializable
{
    public GameObject timeBtn;
    public GameObject changeBtn;
    public GameObject timeDuiHao;
    public GameObject changeDuiHao;
    public Text levelText;
    private int defaultIndex = 0;

    public void Initialize(object data)
    {
        Debug.Log("BoosterPanel initialized" + data);

    }
    public void OnCloseClick()
    {
        // 关闭面板
        PanelManager.Instance.ClosePanel<BoosterPanel>();
    }

    public override void OnOpen()
    {
        base.OnOpen(); // 调用基类的 OnOpen 方法
        GlobalEventManager.OnEvent("AdsPlaybackCompleted", OnAdsPlaybackCompleted);

        // 在面板打开时执行的逻辑
        Debug.Log("BoosterPanel opened");
        StartCoroutine(DelayShowNoThanks());

        timeDuiHao.SetActive(defaultIndex == 0);
        changeDuiHao.SetActive(defaultIndex == 1);
        levelText.text = "Level " + UserManager.Instance.GetUserInfo()["CurrentLv"];
    }

    public override void OnClose()
    {
        base.OnClose(); // 调用基类的 OnClose 方法
        GlobalEventManager.OffEvent("AdsPlaybackCompleted", OnAdsPlaybackCompleted);
        // 在面板关闭时执行的逻辑
        Debug.Log("BoosterPanel closed");
        GlobalEventManager.EmitEvent<int>("ShowTipBg", 1); //显示TipBg
    }

    private IEnumerator DelayShowNoThanks()
    {
        Transform tf = transform.Find("Bg/NoThanks");
        if (tf != null)
        {
            tf.gameObject.SetActive(false);
            yield return new WaitForSeconds(1f);
            tf.gameObject.SetActive(true);
        }
        else
        {
            Debug.LogError("NoThanks not found");
        }
    }
    public void OnTimeBtnClick()
    {
        defaultIndex = 0;
        timeDuiHao.SetActive(true);
        changeDuiHao.SetActive(false);
    }

    public void OnChangeBtnClick()
    {
        defaultIndex = 1;
        timeDuiHao.SetActive(false);
        changeDuiHao.SetActive(true);
    }

    public void OnWatchAdsClick()
    {
        // 点击了看广告按钮
        Debug.Log("WatchAds button clicked");
        bool st = AdsManager.Instance.IsRewardedAdReady();
        if (st)
        {
            AdsManager.Instance.ShowRewardedAd();
        }
        else
        {
            Tools.ShowTipToast("Ad is not Ready");
        }
    }

    public void OnAdsPlaybackCompleted()
    {
        Debug.Log("广告播放完成");
        if (defaultIndex == 0)
        {
            PropsManager.Instance.AddProp(PropsManager.PropType.Prop1, 1);
        }
        else
        {
            PropsManager.Instance.AddProp(PropsManager.PropType.Prop3, 1);
        }
        OnCloseClick();
    }
}
